package service

import (
	"context"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"strings"
)

type _dishService struct{}

var DishService = _dishService{}

func (s _dishService) CreateApi(ctx context.Context, entity *model.DishesEntity) (err error) {
	return dao.DishesEntity.WithContext(ctx).Create(entity)
}

func (s _dishService) BatchCreateDishSpuRatioApi(ctx context.Context, entities []*model.DishesSpuRatioEntity) (err error) {
	err = dao.DishesSpuRatioEntity.WithContext(ctx).CreateInBatches(entities, len(entities))
	return
}

func (s _dishService) ModifyByIDApi(ctx context.Context, id int64, status int64) (err error) {
	_, err = dao.DishesEntity.WithContext(ctx).Where(dao.DishesEntity.ID.Eq(id)).UpdateSimple(dao.DishesEntity.IsEnable.Value(status))
	return
}

func (s _dishService) GetListByNameApi(ctx context.Context, name string) (resp []*model.DishesEntity, err error) {
	return dao.DishesEntity.WithContext(ctx).Where(dao.DishesEntity.Name.Like("%" + name + "%")).
		Or(dao.DishesEntity.Code.Like("%" + name + "%")).Find()
}

func (s _dishService) GetByIDApi(ctx context.Context, id int64) (entities *model.DishesEntity, err error) {
	return dao.DishesEntity.WithContext(ctx).Where(dao.DishesEntity.ID.Eq(id)).First()
}

func (s _dishService) GetPagesApi(ctx context.Context, req *bo.DishPagesReq) (entities []*model.DishesEntity, count int64, err error) {
	tx := dao.DishesEntity.WithContext(ctx).Where(dao.DishesEntity.EnterpriseID.Eq(req.EnterpriseID))
	if req.Name != "" {
		req.Name = strings.ReplaceAll(req.Name, "%", "\\%")
		req.Name = strings.ReplaceAll(req.Name, "_", "\\%")
		tx = tx.Where(dao.DishesEntity.Name.Like("%" + req.Name + "%"))
	}
	if req.Code != "" {
		req.Name = strings.ReplaceAll(req.Name, "_", "\\%")
		tx = tx.Where(dao.DishesEntity.Code.Like("%" + req.Code + "%"))
	}
	if req.IsEnable != 0 {
		tx = tx.Where(dao.DishesEntity.IsEnable.Eq(req.IsEnable))
	}
	offset, limit := utils.DbPage(req.PageNumber, req.PageSize)
	entities, count, err = tx.Order(dao.DishesEntity.ID.Desc()).FindByPage(offset, limit)
	return
}

func (s _dishService) GetSpuRatioApi(ctx context.Context, dishId int64) (entity []*bo.SpuRatioInfo, err error) {
	err = dao.DishesSpuRatioEntity.WithContext(ctx).Select(
		dao.DishesSpuRatioEntity.SpuID, dao.DishesSpuRatioEntity.Percent, dao.SpuEntity.Name.As("Name"),
		dao.UnitEntity.Name.As("UnitName"), dao.SpuEntity.ProductImageURL, dao.SpuEntity.SpuType,
	).
		Where(dao.DishesSpuRatioEntity.DishesID.Eq(dishId)).
		LeftJoin(dao.SpuEntity, dao.DishesSpuRatioEntity.SpuID.EqCol(dao.SpuEntity.ID)).
		LeftJoin(dao.UnitEntity, dao.SpuEntity.UnitID.EqCol(dao.UnitEntity.ID)).Scan(&entity)
	return
}

func (s _dishService) GetDishInfoMapApi(ctx context.Context, dishIDs []int64) (resp map[int64]*model.DishesEntity, err error) {
	dishEntities, err := dao.DishesEntity.WithContext(ctx).Where(dao.DishesEntity.ID.In(dishIDs...)).Find()
	resp = make(map[int64]*model.DishesEntity)
	for _, dishEntity := range dishEntities {
		if _, ok := resp[dishEntity.ID]; !ok {
			resp[dishEntity.ID] = dishEntity
		}
	}
	return
}

func (s _dishService) EditApi(ctx context.Context, entity *model.DishesEntity) (err error) {
	return dao.DishesEntity.WithContext(ctx).Save(entity)
}

func (s _dishService) DeleteDishSpuRatioApi(ctx context.Context, dishID int64) (err error) {
	_, err = dao.DishesSpuRatioEntity.WithContext(ctx).Where(dao.DishesSpuRatioEntity.DishesID.Eq(dishID)).Delete()
	return
}
